/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.risk.service.impl;


import com.koron.common.bean.StaffBean;
import com.koron.common.domain.DataBean;
import com.koron.common.generator.DefaultIdGenerator;
import com.koron.hazardsource.util.RiskLECDesc;
import com.koron.hazardsource.util.RiskLevelDesc;
import com.koron.risk.bean.RelationBean;
import com.koron.risk.bean.SourceQueryBean;
import com.koron.risk.domain.GridNode;
import com.koron.risk.domain.HazardSourceGridRelation;
import com.koron.risk.domain.LocalHazardSource;
import com.koron.risk.domain.LocalRisk;
import com.koron.risk.mapper.GridNodeMapper;
import com.koron.risk.mapper.HazardSourceDistributeMapper;
import com.koron.risk.mapper.LocalHazardSourceMapper;
import com.koron.risk.mapper.LocalRiskMapper;
import com.koron.risk.service.api.HazardSourceDistributeService;
import com.koron.risk.util.BusinessCode;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.swan.bean.MessageBean;

import java.math.BigDecimal;
import java.util.*;

@Service
public class HazardSourceDistributeServiceImpl implements HazardSourceDistributeService {

    private final  static org.apache.log4j.Logger logger = Logger.getLogger(HazardSourceDistributeServiceImpl.class);

    @Autowired
    private GridNodeServiceImpl gridNodeService;

    @SuppressWarnings({"deprecation"})
    @Override
    public MessageBean<?> insert(StaffBean user, List<RelationBean> relationBeanList, String gridCode, String corpCode) {
        MessageBean<?> msg = MessageBean.create(BusinessCode.MESSAGE_CODE_SUCCESS, BusinessCode.MESSAGE_DESCRIPTION_SUCCESS, void.class);
        SessionFactory factory = new SessionFactory();
        try {
            if (StringUtils.isEmpty(gridCode) || StringUtils.isEmpty(corpCode)) {
                msg.setCode(BusinessCode.MESSAGE_CODE_SERVICE_EXCEPTION);
                msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_SERVICE_EXCEPTION);
                factory.close(false);
                return msg;
            }
            if (null==relationBeanList||relationBeanList.size()<=0) {
                msg.setCode(BusinessCode.MESSAGE_CODE_SERVICE_EXCEPTION);
                msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_SERVICE_EXCEPTION);
                factory.close(false);
                return msg;
            }
            List<HazardSourceGridRelation> beanList = new ArrayList<HazardSourceGridRelation>();
            for (RelationBean relation:relationBeanList) {
                HazardSourceGridRelation hsgr = new HazardSourceGridRelation();
                hsgr.setAddTime(new Date());
                hsgr.setAddUser(user.getCode());
                hsgr.setCorpCode(corpCode);
                hsgr.setGridCode(gridCode);
                hsgr.setLocalHazardCode(relation.getCode());
                hsgr.setHazardNumber(relation.getNum());
                hsgr.setManagerCode(relation.getManagerCode());
                hsgr.setManagerName(relation.getManagerName());
                hsgr.setId(DefaultIdGenerator.getInstance().generateLongId());
                Integer count = factory.getMapper(HazardSourceDistributeMapper.class).addCount(corpCode, gridCode, relation.getCode());
                if (count == 0) {
                    beanList.add(hsgr);
                }
            }
            if (beanList.size() > 0) {
                factory.getMapper(HazardSourceDistributeMapper.class).batchInsert(beanList);
            }
            factory.close();
        } catch (Exception e) {
            if (null != factory)
                factory.close(false);
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.MESSAGE_CODE_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_EXCEPTION);
            return msg;
        }
        return msg;
    }

    @SuppressWarnings({"deprecation"})
    @Override
    public MessageBean<?> delete(String code, String gridCode, String corpCode) {
        MessageBean<?> msg = MessageBean.create(BusinessCode.MESSAGE_CODE_SUCCESS, BusinessCode.MESSAGE_DESCRIPTION_SUCCESS, void.class);
        try (SessionFactory factory = new SessionFactory()) {
            if (StringUtils.isEmpty(gridCode) || StringUtils.isEmpty(corpCode) || StringUtils.isEmpty(code)) {
                msg.setCode(BusinessCode.MESSAGE_CODE_SERVICE_EXCEPTION);
                msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_SERVICE_EXCEPTION);
                return msg;
            }
            factory.getMapper(HazardSourceDistributeMapper.class).deleteByCode(corpCode, gridCode, code);
        } catch (Exception e) {
            msg.setCode(BusinessCode.MESSAGE_CODE_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_EXCEPTION);
            logger.error(e.getMessage(),e);
            return msg;
        }
        return msg;
    }


    @SuppressWarnings("deprecation")
    public List<LocalHazardSource> queryList(SourceQueryBean queryBean) {
        try (SessionFactory factory = new SessionFactory();) {
            return factory.getMapper(HazardSourceDistributeMapper.class).list(queryBean);
        }
    }

    @SuppressWarnings({"unchecked", "rawtypes", "deprecation"})
    @Override
    public MessageBean<?> list(SourceQueryBean queryBean) {
        MessageBean<DataBean> msg = MessageBean.create(BusinessCode.MESSAGE_CODE_SUCCESS, BusinessCode.MESSAGE_DESCRIPTION_SUCCESS, DataBean.class);
        Integer totalPage = null;
        Integer totalNumber = null;
        Integer start = null;
        Integer end = null;
        Integer page = queryBean.getPage();
        Integer pageSize = queryBean.getPageSize();
        if (null != page && pageSize != null) {
            if (page > 0 && pageSize > 0) {
            } else {
                //第一页 10条数据
                page = 1;
                pageSize = 10;
            }
            start = (page <= 1) ? 0 : (page - 1) * pageSize;
            end = pageSize;
        }
        queryBean.setStart(start);
        queryBean.setEnd(end);
        try (SessionFactory factory = new SessionFactory();) {
            totalNumber = factory.getMapper(HazardSourceDistributeMapper.class).count(queryBean);
            if (start != null && end != null) {
                totalPage = totalNumber / pageSize + 1;
            } else {
                totalPage = 1;
                page = totalPage;
                pageSize = totalNumber;
            }
            DataBean data = new DataBean();
            List<LocalHazardSource> sources = new ArrayList<>();
            List<LocalHazardSource> localHazardSources = queryList(queryBean);
            if (!localHazardSources.isEmpty()) {
                for (LocalHazardSource one : localHazardSources) {
                    for (int i = 0; i < one.getHazardNumber(); i++) {
                        LocalHazardSource dest = new LocalHazardSource();
                        PropertyUtils.copyProperties(dest, one);
                        dest.setName(dest.getName() + (i + 1) + "#");
                        List<LocalRisk> lrList = factory.getMapper(LocalRiskMapper.class).queryBySourceId("local_risk", dest.getId());
                        Integer temp = 0;
                        String levelName="";
                        for (LocalRisk lr : lrList) {
                            if("LS".equals(one.getIdentificationType())){
                                RiskLevelDesc levelDesc = match(lr.getRiskLevel());
                                lr.setRiskLevelName(levelDesc != null? levelDesc.getName() : "");
                                levelDesc = match(lr.getResidueLevel());
                                lr.setResidueLevelName(levelDesc != null? levelDesc.getName() : "");
                                Integer level = lr.getRiskLevel()== null?0:lr.getRiskLevel();
                                if(level > temp){
                                    temp = level;
                                    levelName = lr.getRiskLevelName();
                                }
                            }else{
                                RiskLECDesc lecDesc=matchRiskLECDesc(new BigDecimal(lr.getRiskLevel()));
                                lr.setRiskLevelName(lecDesc!=null?lecDesc.getName():"");
                                Integer level = lr.getRiskLevel()== null?0:lr.getRiskLevel();
                                if(level > temp){
                                    temp = level;
                                    levelName = lr.getRiskLevelName();
                                }
                            }

                        }
                        dest.setRisks(lrList);
                        if(temp != 0){
                            dest.setRiskLevel(temp);
                            dest.setRiskLevelName(levelName);
                        }
                        sources.add(dest);
                    }
                }
            }
            data.setList(sources);
            data.setPageNo(page);
            data.setPageSize(pageSize);
            data.setTotalNumber(totalNumber);
            data.setTotalPage(totalPage);
            msg.setData(data);
        } catch (Exception e) {
            msg.setCode(BusinessCode.MESSAGE_CODE_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_EXCEPTION);
            return msg;
        }
        return msg;
    }

    @SuppressWarnings("deprecation")
	@Override
	public MessageBean<?> updateNum(String code, String gridCode, int num,String managerCode,String managerName,StaffBean user) {
    	MessageBean<?> msg = MessageBean.create(BusinessCode.MESSAGE_CODE_SUCCESS, BusinessCode.MESSAGE_DESCRIPTION_SUCCESS, void.class);
        try (SessionFactory factory = new SessionFactory()) {
            if (StringUtils.isEmpty(gridCode) || StringUtils.isEmpty(code)||StringUtils.isEmpty(managerCode)||StringUtils.isEmpty(managerName)) {
                msg.setCode(BusinessCode.MESSAGE_CODE_SERVICE_EXCEPTION);
                msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_SERVICE_EXCEPTION);
                return msg;
            }
            factory.getMapper(HazardSourceDistributeMapper.class).updateNum(code, gridCode, num,managerCode,managerName,user.getCode());
        } catch (Exception e) {
            msg.setCode(BusinessCode.MESSAGE_CODE_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_EXCEPTION);
            logger.error(e.getMessage(),e);
            return msg;
        }
        return msg;
    }

	@SuppressWarnings({ "unchecked", "rawtypes", "deprecation" })
	@Override
	public MessageBean<?> irrelevantList(SourceQueryBean queryBean) {
    	MessageBean<DataBean> msg = MessageBean.create(BusinessCode.MESSAGE_CODE_SUCCESS, BusinessCode.MESSAGE_DESCRIPTION_SUCCESS, DataBean.class);
        Integer totalPage = null;
        Integer totalNumber = null;
        Integer start = null;
        Integer end = null;
        Integer page = queryBean.getPage();
        Integer pageSize = queryBean.getPageSize();
        if (null != page && pageSize != null) {
            if (page > 0 && pageSize > 0) {
            } else {
                //第一页 10条数据
                page = 1;
                pageSize = 10;
            }
            start = (page <= 1) ? 0 : (page - 1) * pageSize;
            end = pageSize;
        }
        queryBean.setStart(start);
        queryBean.setEnd(end);
        try (SessionFactory factory = new SessionFactory()) {
        	HazardSourceDistributeMapper mapper = factory.getMapper(HazardSourceDistributeMapper.class);
        	totalNumber = mapper.irrelevantCount(queryBean);
            if (start != null && end != null) {
                totalPage = totalNumber / pageSize + 1;
            } else {
                totalPage = 1;
                page = totalPage;
                pageSize = totalNumber;
            }
            DataBean data = new DataBean();
            data.setList(mapper.irrelevantList(queryBean));
            data.setPageNo(page);
            data.setPageSize(pageSize);
            data.setTotalNumber(totalNumber);
            data.setTotalPage(totalPage);
            msg.setData(data);
        } catch (Exception e) {
            msg.setCode(BusinessCode.MESSAGE_CODE_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_EXCEPTION);
            return msg;
        }
        return msg;
	}

	@SuppressWarnings({"deprecation", "rawtypes","unchecked"})
    @Override
    public MessageBean<?> MatrixToImage(String code, String gridCode, String corpCode) {
        MessageBean<List> msg = MessageBean.create(BusinessCode.MESSAGE_CODE_SUCCESS, BusinessCode.MESSAGE_DESCRIPTION_SUCCESS, List.class);
        SessionFactory factory = new SessionFactory();
        try {
            if (StringUtils.isEmpty(gridCode) && StringUtils.isEmpty(corpCode) && StringUtils.isEmpty(code)) {
                msg.setCode(BusinessCode.MESSAGE_CODE_SERVICE_EXCEPTION);
                msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_SERVICE_EXCEPTION);
                factory.close(false);
                return msg;
            }
            LocalHazardSourceMapper lhsMapper = factory.getMapper(LocalHazardSourceMapper.class);
            GridNodeMapper gnMapper = factory.getMapper(GridNodeMapper.class);

            List ret = new ArrayList<>();
            List<HazardSourceGridRelation> list = factory.getMapper(HazardSourceDistributeMapper.class).query(code, gridCode, corpCode);
            for (HazardSourceGridRelation hsr : list) {
                String gridName = "";
                String lhsName = "";

                LocalHazardSource lhs = lhsMapper.queryByCode("local_hazard_source", hsr.getLocalHazardCode());
                //查询危险源
                if (lhs != null) {
                    lhsName = lhs.getName();
                    code = lhs.getCode();
                }
                //查询网格
                GridNode gn = gnMapper.queryById(hsr.getGridCode());
                if (gn == null) {
                    //查询部门
                    MessageBean deptMsg = gridNodeService.childList2(hsr.getCorpCode(),hsr.getGridCode(), 0);
                    if (deptMsg.getData() != null) {
                        List<Map> deptList = (List) deptMsg.getData();
                        Map deptMap = deptList.get(0);
                        gridName = (String) deptMap.get("name");
                        gridCode = (String) deptMap.get("id");
                    }
                } else {
                    gridName = gn.getName();
                    gridCode = gn.getId();
                }
                //查询公司
//				CorpBean corp = corpMapper.get(hsr.getCorpCode());
//				if(corp != null){
//					corpName = corp.getCorpName();
//				}
                factory.close();
                int num = hsr.getHazardNumber();
                for (int i = 1; i <= num; i++) {
                    StringBuffer textStrBuffer = new StringBuffer();
                    StringBuffer nameStrBuffer = new StringBuffer();

                    //前三位自定义类型编码 —网格id—危险源code—序号
                    textStrBuffer.append("LHS_").append(gridCode).append("_").append(code).append("_" + i);
                    nameStrBuffer.append(gridName).append(lhsName).append(i + "#");
                    Map map = new HashMap();
                    map.put("text", textStrBuffer.toString());
                    map.put("name", nameStrBuffer.toString());
                    ret.add(map);
                }
            }
            msg.setData(ret);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            if (null != factory)
                factory.close(false);
            msg.setCode(BusinessCode.MESSAGE_CODE_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_EXCEPTION);
            return msg;
        }
        return msg;
    }



	@Override
	public MessageBean<?> childAllByGrid(SourceQueryBean queryBean) {
		MessageBean<DataBean> msg = MessageBean.create(0,"SUCCESS", DataBean.class);
		String gridCode = queryBean.getGridCode();
		String corpCode = queryBean.getCorpCode();
		if(StringUtils.isEmpty(gridCode) || StringUtils.isEmpty(corpCode)){
			return msg;
		}
		Integer totalPage = null;
        Integer totalNumber = null;
        Integer start = null;
        Integer end = null;
        Integer page = queryBean.getPage();
        Integer pageSize = queryBean.getPageSize();

        if (null != page && pageSize != null) {
            if (page > 0 && pageSize > 0) {
            } else {
                //第一页 10条数据
                page = 1;
                pageSize = 10;
            }
            start = (page <= 1) ? 0 : (page - 1) * pageSize;
            end = pageSize;
        }
        queryBean.setStart(start);
        queryBean.setEnd(end);
        Map<String,Map<String,List<String>>> childMap = gridNodeService.getChildMap();
        List<String> idList = null;
        Map<String,List<String>> childAllMap = childMap.get(corpCode);
        if(childAllMap == null){
        	childAllMap = new HashMap<String,List<String>>();
        	idList = gridNodeService.childAll(gridCode,new ArrayList<>());  //下级网格id(不包含当前网格)
        	childAllMap.put(gridCode, idList);
        	childMap.put(corpCode, childAllMap);
        }else{
        	idList = childAllMap.get(gridCode); //缓存中取当前网格下所有子节点
        	if(idList == null || idList.isEmpty()){
        		idList = gridNodeService.childAll(gridCode,new ArrayList<>());  //下级网格id(不包含当前网格)
        		childAllMap.put(gridCode, idList);
            }
        }
        List<String> allId = new ArrayList<String>();
        allId.addAll(idList);
        allId.add(gridCode); //把当前网格加入
        try(SessionFactory factory = new SessionFactory()) {
        	HazardSourceDistributeMapper mapper = factory.getMapper(HazardSourceDistributeMapper.class);
        	queryBean.setIdList(allId);
        	totalNumber = mapper.countAllByGrid(queryBean);  //总条数
        	//当前网格关联的危险源
        	List<LocalHazardSource> list = mapper.childListByGrid(queryBean);
        	if(start != null && end != null){
				totalPage = (totalNumber+pageSize-1)/pageSize;
			}else{
				totalPage = 1;
				page = totalPage;
				pageSize = totalNumber;
			}
        	for (LocalHazardSource lhs : list) {
        		List<LocalRisk> lrList = factory.getMapper(LocalRiskMapper.class).queryBySourceId("local_risk", lhs.getId());
				Integer temp = 0;
				String levelName="";
				for (LocalRisk lr : lrList) {
                    if("LS".equals(lhs.getIdentificationType())){
                        RiskLevelDesc levelDesc = match(lr.getRiskLevel());
                        lr.setRiskLevelName(levelDesc != null? levelDesc.getName() : "");
                        levelDesc = match(lr.getResidueLevel());
                        lr.setResidueLevelName(levelDesc != null? levelDesc.getName() : "");
                        Integer level = lr.getRiskLevel()== null?0:lr.getRiskLevel();
                        if(level > temp){
                            temp = level;
                            levelName = lr.getRiskLevelName();
                        }
                    }else{
                        RiskLECDesc lecDesc=matchRiskLECDesc(new BigDecimal(lr.getRiskLevel()));
                        lr.setRiskLevelName(lecDesc!=null?lecDesc.getName():"");
                        Integer level = lr.getRiskLevel()== null?0:lr.getRiskLevel();
                        if(level > temp){
                            temp = level;
                            levelName = lr.getRiskLevelName();
                        }
                    }
				}
				lhs.setRisks(lrList);
				if(temp != 0){
					lhs.setRiskLevel(temp);
					lhs.setRiskLevelName(levelName);
				}
			}
            DataBean data = new DataBean();
            data.setList(list);
            data.setPageNo(page);
            data.setPageSize(pageSize);
            data.setTotalNumber(totalNumber);
            data.setTotalPage(totalPage);
            msg.setData(data);
		} catch (Exception e) {
            msg.setCode(BusinessCode.MESSAGE_CODE_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_EXCEPTION);
            return msg;
		}
		return msg;
	}

	@SuppressWarnings({ "rawtypes", "unchecked", "deprecation" })
	@Override
	public MessageBean<?> childAll(SourceQueryBean queryBean) {
		MessageBean<DataBean> msg = MessageBean.create(0,"SUCCESS", DataBean.class);
		String gridCode = queryBean.getGridCode();
		String corpCode = queryBean.getCorpCode();

		if(StringUtils.isEmpty(gridCode) || StringUtils.isEmpty(corpCode)){
			return msg;
		}


		Integer totalPage = null;
        Integer totalNumber = null;
        Integer start = null;
        Integer end = null;
        Integer page = queryBean.getPage();
        Integer pageSize = queryBean.getPageSize();

        if (null != page && pageSize != null) {
            if (page > 0 && pageSize > 0) {
            } else {
                //第一页 10条数据
                page = 1;
                pageSize = 10;
            }
            start = (page <= 1) ? 0 : (page - 1) * pageSize;
            end = page * pageSize;
        }
        Map<String,Map<String,List<String>>> childMap = gridNodeService.getChildMap();
        List<String> idList = null;
        Map<String,List<String>> childAllMap = childMap.get(corpCode);
        if(childAllMap == null){
        	childAllMap = new HashMap<String,List<String>>();
        	idList = gridNodeService.childAll(gridCode,new ArrayList<>());  //下级网格id(不包含当前网格)
        	childAllMap.put(gridCode, idList);
        	childMap.put(corpCode, childAllMap);
        }else{
        	idList = childAllMap.get(gridCode); //缓存中取当前网格下所有子节点
        	if(idList == null || idList.isEmpty()){
        		idList = gridNodeService.childAll(gridCode,new ArrayList<>());  //下级网格id(不包含当前网格)
        		childAllMap.put(gridCode, idList);
            }
        }

        List<String> allId = new ArrayList<String>();
        allId.addAll(idList);
        allId.add(gridCode); //把当前网格加入
        try(SessionFactory factory = new SessionFactory()) {
        	HazardSourceDistributeMapper mapper = factory.getMapper(HazardSourceDistributeMapper.class);
        	queryBean.setIdList(allId);
        	totalNumber = mapper.countAll(queryBean);  //总条数
        	//当前网格关联的危险源
        	List<LocalHazardSource> list = queryList(queryBean);
        	queryBean.setIdList(idList);
        	//子网格关联的危险源列表
        	if(!idList.isEmpty()){
        		list.addAll(mapper.childList(queryBean));
        	}
        	if(start != null && end != null){ //需要分页
        		totalPage = (totalNumber+pageSize-1)/pageSize;
        		if(end > totalNumber){
        			end = totalNumber;
        		}
        		list = list.subList(start, end);
        	}else{
        		totalPage = 1;
                page = totalPage;
                pageSize = totalNumber;
        	}

        	Map<String,String> positionMap = gridNodeService.getPositionMap();

        	for (LocalHazardSource lhs : list) {
        		List<LocalRisk> lrList = factory.getMapper(LocalRiskMapper.class).queryBySourceId("local_risk", lhs.getId());
				Integer temp = 0;
				String levelName="";
				for (LocalRisk lr : lrList) {
                    if("LS".equals(lhs.getIdentificationType())){
                        RiskLevelDesc levelDesc = match(lr.getRiskLevel());
                        lr.setRiskLevelName(levelDesc != null? levelDesc.getName() : "");
                        levelDesc = match(lr.getResidueLevel());
                        lr.setResidueLevelName(levelDesc != null? levelDesc.getName() : "");
                        Integer level = lr.getRiskLevel()== null?0:lr.getRiskLevel();
                        if(level > temp){
                            temp = level;
                            levelName = lr.getRiskLevelName();
                        }
                    }else{
                        RiskLECDesc lecDesc=matchRiskLECDesc(new BigDecimal(lr.getRiskLevel()));
                        lr.setRiskLevelName(lecDesc!=null?lecDesc.getName():"");
                        Integer level = lr.getRiskLevel()== null?0:lr.getRiskLevel();
                        if(level > temp){
                            temp = level;
                            levelName = lr.getRiskLevelName();
                        }
                    }
				}
				lhs.setRisks(lrList);
				if(temp != 0){
					lhs.setRiskLevel(temp);
					lhs.setRiskLevelName(levelName);
				}

        		String nodeId = lhs.getGridCode();
        		String nodeName = positionMap.get(nodeId);
        		if(StringUtils.isEmpty(nodeName)){
        			nodeName = gridNodeService.getPosition(nodeId, corpCode, new StringBuffer());
        			positionMap.put(nodeId, nodeName);
        		}
        		lhs.setGridName(nodeName);
			}
            DataBean data = new DataBean();
            data.setList(list);
            data.setPageNo(page);
            data.setPageSize(pageSize);
            data.setTotalNumber(totalNumber);
            data.setTotalPage(totalPage);
            msg.setData(data);
		} catch (Exception e) {
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.MESSAGE_CODE_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_EXCEPTION);
            return msg;
		}
		return msg;
	}

	private RiskLevelDesc match(Integer riskLevel) {
        for (Map.Entry<RiskLevelDesc, List<Integer>> entry : levelMap.entrySet()) {
            RiskLevelDesc key = entry.getKey();
            List<Integer> value = entry.getValue();
            if (value.contains(riskLevel)) {
                return key;
            }
        }
        return null;
    }

    private RiskLECDesc matchRiskLECDesc(BigDecimal riskLevel){
        if (riskLevel.compareTo(BigDecimal.ZERO)==1&&riskLevel.compareTo(new BigDecimal(20))==-1){
            return RiskLECDesc.LOW;
        }else if (riskLevel.compareTo(new BigDecimal(70))==-1){
            return RiskLECDesc.Minor;
        }else if (riskLevel.compareTo(new BigDecimal(160))==-1){
            return RiskLECDesc.Ordinary;
        }else if (riskLevel.compareTo(new BigDecimal(320))==-1){
            return RiskLECDesc.Serious;
        } else if (riskLevel.compareTo(new BigDecimal(320))==1){
            return RiskLECDesc.Important;
        }else{
            return null;
        }
    }

	private static Map<RiskLevelDesc, List<Integer>> levelMap = new HashMap<>();

	static {
        levelMap.put(RiskLevelDesc.Minor, Arrays.asList(1, 2, 3, 4));
        levelMap.put(RiskLevelDesc.Ordinary, Arrays.asList(5, 6, 8, 9));
        levelMap.put(RiskLevelDesc.Serious, Arrays.asList(10, 12, 15, 16));
        levelMap.put(RiskLevelDesc.Important, Arrays.asList(20, 25));
    }
}
